package com.filemanager;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: Anna.Bulenkova
 * Date: 27.10.11
 * Time: 13:57

 */

public class JavaToXMLUsingJAXB {

    public static  void sendXML(PrintWriter out) throws Exception {
        /**
         * Create a context with a package name of where objects are located
         */
        MyJAXBObject object = new MyJAXBObject();
        JAXBContext context = JAXBContext.newInstance(object.getClass());

        /**
         * Create an XML Marshaller
         */
        Marshaller m = context.createMarshaller();
        /**
         * Set the Marshaller property
         * Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE formats output XML
         */
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        /**
         * Set the object properties
         */
        object.setMyField1("Anja");
        object.setMyField2("B");
        object.setMyIntegerField(888);
        /**
         * Marshal the object to the console.
         */
        m.marshal(object, System.out);
        /**
         * Marshalling the same object to the physical file
         */
//        m.marshal(object, new FileOutputStream("jaxbobject.xml"));
        m.marshal(object, out);

//        new FileOutputStream()
      }
}

